package mkis.service.root.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mkis.service.root.common.PageInfo;
import mkis.service.root.common.SearchInfo;
import mkis.service.root.constants.Constants;
import mkis.service.root.data.model.JsonTemplete;
import mkis.service.root.data.model.Member;
import mkis.service.root.data.model.OrgParam;
import mkis.service.root.data.model.SampleJson;
import mkis.service.root.service.MemberService;
import mkis.service.root.utils.MkisUtils;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class OrgCTR {
	
	@Resource(name="memberService")
	private MemberService memberService;
	
	
	@RequestMapping(value="/org/org_list.do")
	public ModelAndView orgList(HttpServletRequest request,HttpServletResponse response,String dept,PageInfo pageInfo,SearchInfo searchInfo,Integer mb_level) throws Exception{
		ModelAndView mav=new ModelAndView("org/org_list");
		String page=pageInfo.getPage();
		
		String typeSelect=searchInfo.getTypeSelect();
		String input=searchInfo.getInput();
		if(page==null ||"".equals(page)){
			page="1";
		}
		if(mb_level==null){
			mb_level=1;
		} 
		int startRow=(Integer.parseInt(page)-1)*6;
		
		Map<String, Object> paramMap=new HashMap<String, Object>();
		
		paramMap.put("input", input);
		paramMap.put("typeSelect", typeSelect);
		paramMap.put("dept", dept);
		paramMap.put("page", Integer.parseInt(page));
		paramMap.put("startRow", startRow);
		paramMap.put("rowcnt", Constants.rowcnt);
		paramMap.put("mb_level", mb_level);
		
		
		String mb_dept_code=dept;
		
		paramMap.put("mb_dept_code",mb_dept_code);
		int mb_auth_code=MkisUtils.getMember(request).getMb_auth_code();
		
		
		
		/*if(mb_auth_code!=7 && mb_auth_code!=8) {
			paramMap.put("mb_dept_code",MkisUtils.getMember(request).getMb_dept_code());
			
		}*/
		
		
		Map<String,Object> map=memberService.getMemberListAndCnt(paramMap);
		
		List<Member>list=(List<Member>) map.get("list");
		int cnt=(Integer) map.get("totalcnt");
		
		mav.addObject("mb_level", mb_level);
		mav.addObject("input", input);
		mav.addObject("typeSelect", typeSelect);
		mav.addObject("dept", dept);
		mav.addObject("page",  Integer.parseInt(page));
		mav.addObject("list", list);
		mav.addObject("totalcnt", cnt);
		
		return mav;
	
	}
	
	@RequestMapping(value="/org/org_view.do")
	public ModelAndView orgView(HttpServletRequest request,HttpServletResponse response,String dept,String mb_id) throws Exception{
		ModelAndView mav=new ModelAndView("org/org_view");
		Map<String,Object> queryParam=new HashMap<String, Object>();
		queryParam.put("mb_id",mb_id);
		Member member=memberService.getMember(queryParam);
		
		mav.addObject("member", member);
		return mav;
	}
	

	@RequestMapping(value="/org/emp_out.do")
	public ModelAndView emp_out(HttpServletRequest request,HttpServletResponse response,String dept,String mb_id) throws Exception{
		ModelAndView mav=new ModelAndView("org/org_out");
		Map<String,Object> queryParam=new HashMap<String, Object>();
		queryParam.put("mb_id",mb_id);
		queryParam.put("mb_level",0);
		
		memberService.outMember(queryParam);
		
		return mav;
	}
	
	@RequestMapping(value="/org/orglist_check.do")
	public @ResponseBody JsonTemplete orgListCheckResultJSON(OrgParam orgParam) throws Exception  {
	    JsonTemplete jsonTemplete=new JsonTemplete();
	    
	    String resultCode=null;
	    String resultName=null;
	    
	    return memberService.checkMemberParamName(orgParam, jsonTemplete, resultCode, resultName);
	}

	
	
	
	
	
}
